﻿/**
 * Example View Class - just a basic framework for this class
 *
 * The View of the MVC Triad 
 *
 * @author  	robert turrall
 * @date	   	31.12.2008
 * @license: 	This particular file may be used freely. It serves simply as an example.
 * 				Of course, a credit is always nice ;-)
 *
 * @version	1.0
 */
package { 

	// please note that it's not really good practice to use the default package (i.e. none). 
	// You should normally save your classes in a package which corresponds to your application
	
	import net.turrall.mvc.*;
	//import your other package classes as required here
	import flash.display.MovieClip;
	import flash.events.*;    
	
	
	public class ExampleView extends AbstractView {
		
		private var _controller:ExampleController;
		private var _model:ExampleModel;
		
		private var _targetMovie:MovieClip;
		
		private var _config:Object;
		
		public static const VIEW_CHANGED:String = "ViewChanged";
		
		
		public function ExampleView(model:ExampleModel, controller:ExampleController, targetMovie:MovieClip) {	
		
			// no need to create view as it's created already in the movieclip
			super(model, controller);
			_controller = ExampleController(controller);
			_model = ExampleModel(model);
			
			_targetMovie = targetMovie;
			
			init();
		}
		
		
		private function init():void {
			
			trace("_targetMovie is "+_targetMovie);
			
		} // initialise
		
		
		
		// you will probably define various methods here to build the UI and show updated values, etc
		
		/**
		 * example method to trigger model changes in response to
		 * a change in the UI. A control in the UI will invoke this
		 *
		 */
		public function myFunction(val:Number):void {
			this.addEventListener(VIEW_CHANGED, _controller.changedUI);
			
			var obj:Object = new Object();
			obj.changedProperty = "val"; // changedProperty indicates to the Model what changed
			obj.val = val;
			
			var e:CustomEvent = new CustomEvent(VIEW_CHANGED, true, false, obj);
			dispatchEvent(e);
		}
					
		
		
		/**
		 * Updates the state of the on-screen UI.
		 * Invoked automatically by the Model/AbstractModel.
		 *
		 * @param   o   The Model object that is broadcasting an update.
		 * @param   infoObj   An object instance describing the changes that
		 *                    have occurred in the PensCalcModel.
		 */
		override public function update (e:CustomEvent):void {
			
			trace("caught an event "+e);
			
			switch(e.type) {
				
				case ExampleModel.MODEL_INITIALISE:
					// get the config data
					_config = _model.config;
					
					// then build and initialise your UI
					
					break;
				
				case ExampleModel.MODEL_INITERROR:
					
					trace("model init error!");
					break;
				
				case AbstractModel.UPDATE:
					// updated values received from the model (functionality from base abstractmodel)
					// event object will contain values 
					
					// update your UI here, showing new data values
					
					break;
			}
		}
	}
}